package com.example.lx5_22

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.example.base.mvvm.BaseViewActivity
import com.example.lx5_22.databinding.ActivitySplashBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.*
import kotlin.concurrent.timerTask

@Route(path = "/app/SplashActivity")
class SplashActivity : BaseViewActivity<ActivitySplashBinding>() {

    var timer: Timer? = null
    var num = 5
    override fun initView() {
        if(SPUtils.getInstance().getBoolean("isIn",false)){
            ARouter.getInstance().build("/app/MainActivity").navigation()
            finish()
        }

        timer = Timer()
        lifecycleScope.launch(Dispatchers.IO){
            timer!!.schedule(timerTask {
                lifecycleScope.launch(Dispatchers.Main){
                    binding.tvTime.setText("${num}s")
                    if(num == 0){
                        start()
                    }
                    num--
                }
            },0,1000)
        }

        binding.splashSkip.setOnClickListener {
            start()
        }

    }

    private fun start() {
        timer!!.cancel()
        SPUtils.getInstance().put("isIn",true)
        ARouter.getInstance().build("/app/MainActivity").navigation()
        finish()
    }

    override fun initData() {
    }

    override fun onDestroy() {
        super.onDestroy()
        if(timer != null){
            timer?.cancel()
            timer = null
        }
    }
}